//
//  DengLuViewController.m
//  维度科技
//
//  Created by 刘敏 on 2019/1/25.
//  Copyright © 2019 谷银行. All rights reserved.
//

#import "DengLuViewController.h"
#import "RSAEncryptor.h"
@interface DengLuViewController (){
    UIView *SjView;
    UIView *MiView;
    UIImageView *BeiImg;
    UIButton *phoneBun;
    UITextField *phoneTF;
    UIButton *miBun;
    UITextField *miTF;
    UIButton *ZcBun;
    UIButton *logBun;
}

@end

@implementation DengLuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self Denglu];
    [self Sdauto];
    
}
- (void)Denglu{
    //    //设置背景图片
    BeiImg=[[UIImageView alloc]initWithFrame:self.view.frame];
    BeiImg.image=[UIImage imageNamed:@"Bitmap"];
    [self.view addSubview:BeiImg];
    
    // 手机号View
    SjView = [[UIView alloc]init];
    SjView.layer.borderColor=[UIColor whiteColor].CGColor;
    SjView.layer.borderWidth=1;
    [self.view addSubview:SjView];
    // 密码View
    MiView = [[UIView alloc]init];
    MiView.layer.borderColor=[UIColor whiteColor].CGColor;
    MiView.layer.borderWidth=1;
    [self.view addSubview:MiView];
    
    //手机号登录按钮
    phoneBun=[[UIButton alloc]init];
    [phoneBun setImage:[UIImage imageNamed:@"login_icon_phone_n_hdpi"] forState:UIControlStateNormal];
    [SjView addSubview:phoneBun];
    
    phoneTF=[[UITextField alloc]init];
    phoneTF.placeholder=@"手机号";

    phoneTF.keyboardType=UIKeyboardTypeNumberPad;
    [SjView addSubview:phoneTF];
    
    //设置密码
    miBun=[[UIButton alloc]init];
    [miBun setImage:[UIImage imageNamed:@"login_icon_lock_n_hdpi"] forState:UIControlStateNormal];
    [MiView addSubview:miBun];
    
    
    miTF=[[UITextField alloc]init];
    miTF.placeholder=@"登录密码";
    miTF.secureTextEntry = YES;
    [MiView addSubview:miTF];
    
    ZcBun = [[UIButton alloc]init];
    ZcBun.font = [UIFont systemFontOfSize:13];
    [ZcBun setTitle:@"快速注册" forState:UIControlStateNormal];
    [ZcBun addTarget:self action:@selector(ZCBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:ZcBun];
    
    logBun=[[UIButton alloc]init];
    logBun.backgroundColor=[UIColor purpleColor];
    logBun.layer.cornerRadius=10;
    logBun.layer.masksToBounds=YES;
    [logBun setTitle:@"登录" forState:UIControlStateNormal];
    [logBun addTarget:self action:@selector(DLBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:logBun];
}
- (void)Sdauto{
    
    SjView.sd_layout
    .topSpaceToView(self.view, 250)
    .leftSpaceToView(self.view, (Width - 280)/2)
    .heightIs(50)
    .widthIs(280);
    
    
    MiView.sd_layout
    .topSpaceToView(SjView, 50)
    .leftSpaceToView(self.view, (Width - 280)/2)
    .heightIs(50)
    .widthIs(280);
    
    //手机号图片
    phoneBun.sd_layout
    .topSpaceToView(SjView, 10)
    .leftSpaceToView(SjView, 10)
    .widthIs(30)
    .heightIs(30);
    //手机号
    phoneTF.sd_layout
    .topSpaceToView(SjView, 10)
    .leftSpaceToView(phoneBun, 10)
    .widthIs(200)
    .heightIs(30);
    //密码图片
    miBun.sd_layout
    .topSpaceToView(MiView, 10)
    .leftSpaceToView(MiView, 10)
    .widthIs(30)
    .heightIs(30);
    //密码
    miTF.sd_layout
    .topSpaceToView(MiView, 10)
    .leftSpaceToView(miBun, 10)
    .widthIs(200)
    .heightIs(30);
    
    // 注册按钮
    ZcBun.sd_layout
    .topSpaceToView(MiView, 10)
    .rightSpaceToView(self.view, 40)
    .heightIs(30)
    .widthIs(100);
    
    //登录按钮
    logBun.sd_layout
    .bottomSpaceToView(self.view, 150)
    .leftSpaceToView(self, (Width -280)/2)
    .widthIs(280)
    .heightIs(50);
    
}
- (void)DLBtn{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
    securityPolicy.validatesDomainName = NO;
    securityPolicy.allowInvalidCertificates = YES;
    manager.securityPolicy = securityPolicy;
    NSString *SJstr =phoneTF.text;
    NSString *originalString = miTF.text;
    //使用字符串格式的公钥私钥加密解密
    NSString *encryptStr = [RSAEncryptor encryptString:originalString publicKey:@"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCuQd3gESx7VdIyRYUWjmjg61VIgUK6F45hClmqUMZ7xNiT5rlLM6e78osMvBF/yP7cVm7pK+NMCDWoVS1/AETpxJYqUlIC77ZAU8/MnP96IupnJL87vqhPcpdv7+VqLM38ls++yuD/F/HSoOQTv/leJh+dgE/4EYAJjOWFAbYfXwIDAQAB"];
    //参数 电影
    NSDictionary *dic = @{
                          @"phone":SJstr,
                          @"pwd":encryptStr,
                          };
    [manager POST:@"https://172.17.8.100/techApi/user/v1/login" parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"数据有,%@",responseObject);
        UsderModel *model = [UsderModel yy_modelWithJSON:responseObject];
        [[DataManager sharedDataHandle]addOneMovie:model.result];
        if ([responseObject[@"message"] isEqualToString:@"登录成功"]) {
            MainViewController *main = [MainViewController new];
            [self presentViewController:main animated:YES completion:nil];
            
        }else{
            UIAlertController *alert =[UIAlertController alertControllerWithTitle:@"提示" message:@"失败" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *action  = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}
- (void)ZCBtn{
    ZhuceViewController *zhuce = [ZhuceViewController new];
    [self presentViewController:zhuce animated:YES completion:nil];
}


@end
